package com.example.smartagrisys.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.smartagrisys.entity.Product;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 产品Mapper接口
 */
@Mapper
public interface ProductMapper extends BaseMapper<Product> {
    
    /**
     * 获取农户的所有活跃产品（排除肥料商产品）
     * 通过联表查询sys_user表，确保只返回角色为farmer的用户的产品
     */
    @Select("SELECT p.* FROM product p " +
            "JOIN sys_user u ON p.farmer_id = u.id " +
            "WHERE p.status = 1 AND p.stock > 0 AND u.role = 'farmer' " +
            "ORDER BY p.create_time DESC")
    List<Product> selectAllActiveFarmerProducts();
} 